<?php
/**
* Prints links to directories and folders.
* @author Bimal Poudel
*/
class linker
{

	public function __construct()
	{
	}

	/**
	* Print link to a directory
	*/
	function regular_directory($full_path='', $dir_name='')
	{
		$full_path = str_replace('\\', '/', $full_path); # Linux compatible
	
		# Where to search for index.php?
		# The order of listing is important
		$index_links = array(
			# Leased auto listing support
			# This means, project wants our customized auto listng facilities
			# See "index-leased.php" for details.
			# So, we prioritized it.
			'auto-listing.php',

			# usual index.php in the application's parent folder
			'index.php',

			# deeper: search for default index.php
			'public_html/index.php',
			'htdocs/index.php',
			'httpdocs/index.php',
			'httpsdocs/index.php',
			'www/index.php',
		);
		
		$class = 'inactive';
		$link_name = rawurlencode($dir_name).'/'; # Handle spaces and other characters
		foreach($index_links as $il => $link)
		{
			if(is_file($full_path.'/'.$link))
			{
				$class = 'active';
				$link_name = $dir_name.'/'.$link;
				break;
			}
		}
		
		$title = ucwords($dir_name);

		$dir_name = preg_replace('/^[^a-z0-9]+/i', '',   $dir_name);
		$dir_name = preg_replace('/[^a-z0-9]+$/i', '',   $dir_name);
		$dir_name = preg_replace('/[^a-z0-9\.]+/i', ' ', $dir_name);

		$show_name  = strtoupper($dir_name);

		# See info/info.xml for a structure of this information file.
		$info = new info($full_path.'/info.xml');
		
		# If info.xml is missing, the directly will be considered as NOT disabled.
		# If info.xml tells disabled = no, it is considered as NOT disabled.
		# If info.xml tells disabled = yes, it is considered as DISABLED, and we skip linking it.
		if(!$info->is_disabled())
		{
			if($info->is_valid())
			{
				if($info->do_rename())
				{
					$class = 'modified'; # Directry was renamed via info.xml file
					$show_name = $info->name;
				}
				$title = htmlentities($info->comments);
			}
			return "<li class=\"{$class}\"><a href='{$link_name}' title='{$title}'>{$show_name}</a></li>";
		}
		return null;
	}
	
	/**
	* Print individual file link
	*/
	function regular_file($file_name='')
	{
		$show_name = $file_name;
		$show_name = preg_replace('/[^a-z0-9\.]+/i', ' ', $show_name);
		$show_name = preg_replace('/\.php$/i', '', $show_name);
		$show_name = strtoupper($show_name);
		$link_name = rawurlencode($file_name); # Handle spaces and other characters
		return "<li class=\"file\"><a href='{$link_name}' title='{$file_name}'>{$show_name}</a></li>";
	}
}
?>